using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;

[System.Serializable]
public class ModifierPreset {

	[SerializeField] string modifierType;
	[SerializeField] string[] parameters;

	static System.Type[] callConstructorTypes = new System.Type[2];
	static object[] callParams= new object[2];
	static ModifierPreset() {
		callConstructorTypes[0]=typeof(Projectile);
		callConstructorTypes[1]=typeof(string[]);
	}
	bool inited;
	ConstructorInfo constructor;
	System.Type type;
	
	void TryInit(){
		if(!inited) {
			inited=true;
			if(parameters.Length==0) parameters=null;
			type=System.Type.GetType(modifierType);
			System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(type.TypeHandle);
			constructor=type.GetConstructor(callConstructorTypes);
		}
	}
	
	public ProjectileModifier CreateProjectileModifier(Projectile projectile) {
		TryInit();
		callParams[0]=projectile;
		callParams[1]=parameters;
		if(constructor==null) throw new System.Exception("CANNOT FIND CONSTRUCTOR");
		ProjectileModifier newModifier = constructor.Invoke(callParams) as ProjectileModifier;
		projectile.AddModifier(newModifier);
		return newModifier;
	}

	public string GetDescription(string language){
		TryInit();
		return ProjectileModifier.GetDescription(type,parameters,language);
	}

}